﻿using System;
using System.IO;
using Kent.Boogaart.Gauntlet;
using Kent.Boogaart.Gauntlet.RegenerationPolicies;
using NUnit.Framework;

namespace Kent.Boogaart.UnitTest.Gauntlet.RegenerationPolicies
{
	[TestFixture]
	public sealed class TimestampRegenerationPolicyTest
	{
		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void ShouldRegenerate_ShouldThrowIfContextIsNull()
		{
			TimestampRegenerationPolicy policy = new TimestampRegenerationPolicy();
			policy.ShouldRegenerate(null);
		}

		[Test]
		public void ShouldRegenerate_ShouldReturnFalseIfOutputNewerThanInputAndTemplate()
		{
			TimestampRegenerationPolicy policy = new TimestampRegenerationPolicy();
			FileInfo inputFile = GetTempFile();
			FileInfo templateFile = GetTempFile();
			FileInfo outputFile = GetTempFile();
			Context context = new Context() { InputFile = inputFile, OutputFile = outputFile, TemplateFile = templateFile, Timestamp = DateTime.Now };
			Assert.IsFalse(policy.ShouldRegenerate(context));
		}

		[Test]
		public void ShouldRegenerate_ShouldReturnTrueIfOutputOlderThanInput()
		{
			TimestampRegenerationPolicy policy = new TimestampRegenerationPolicy();
			FileInfo templateFile = GetTempFile();
			FileInfo outputFile = GetTempFile();
			FileInfo inputFile = GetTempFile();
			Context context = new Context() { InputFile = inputFile, OutputFile = outputFile, TemplateFile = templateFile, Timestamp = DateTime.Now };
			Assert.IsTrue(policy.ShouldRegenerate(context));
		}

		[Test]
		public void ShouldRegenerate_ShouldReturnTrueIfOutputOlderThanTemplate()
		{
			TimestampRegenerationPolicy policy = new TimestampRegenerationPolicy();
			FileInfo inputFile = GetTempFile();
			FileInfo outputFile = GetTempFile();
			FileInfo templateFile = GetTempFile();
			Context context = new Context() { InputFile = inputFile, OutputFile = outputFile, TemplateFile = templateFile, Timestamp = DateTime.Now };
			Assert.IsTrue(policy.ShouldRegenerate(context));
		}

		private static FileInfo GetTempFile()
		{
			return new FileInfo(Path.GetTempFileName());
		}
	}
}
